﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using EfSearchModel.Model;

namespace QuickApp.Core
{
    public class CrudService<T> : ICrudService<T> where T : class
    {
        protected readonly IRepository<T> repository;
        protected readonly IUnitOfWork unitOfWork;
        public CrudService(IRepository<T> repository, IUnitOfWork unitOfWork)
        {
            this.repository = repository;
            this.unitOfWork = unitOfWork;
        }

        public void Create(T entity)
        {
            repository.Insert(entity);
            unitOfWork.Commit();
        }

        public void Save(T entity)
        {
            repository.Update(entity);
            unitOfWork.Commit();
        }

        public void Delete(string id)
        {
            repository.Delete(id);
            unitOfWork.Commit();
        }

        public void Delete(Func<T, bool> predicate)
        {
            repository.Delete(predicate);
            this.unitOfWork.Commit();
        }

        public T Get(string id)
        {
            return repository.Get(id);
        }

        public IQueryable<T> Get(Expression<Func<T, bool>> predicate)
        {
            var list = repository.Get(predicate);
            return list;
        }

        public int Count()
        {
            return repository.Count();
        }

        public IPageable<T> GetPageable(PageInfo pageInfo, QueryModel query)
        {
            return repository.GetPageable(pageInfo, query);
        }
    }
}
